use dataset_polls, clear

format date %dm_Y

local c2000a = date("11-07-2000", "MDY")
local c2000b = date("12-12-2000", "MDY")
local c2000c = date("11-02-2004", "MDY")
local c2016 = date("11-08-2016", "MDY")
local c2020 = date("11-03-2020", "MDY")

forvalues i = 1(1)3 {

preserve

	if `i' == 1 {
		gen x = date - `c2000b'
		local c2000c = `c2000c' - `c2000b'
		keep if year(date) < 2005
		local mytitle "(a) 2000"
	}	
	if `i' == 2 {
		keep if year(date) > 2005 & year(date) < 2020
		gen x = date - `c2016'
		local mytitle "(b) 2016"
	}
	if `i' == 3 {
		keep if year(date) > 2019
		gen x = date - `c2020'
		local mytitle "(c) 2020"
	}
	
	#delimit;

	gr tw
		(sc legitimatec x if subsample == "All", col(gray) 
			msym(O) mfcol(white))
		(lfit legitimatec x if regex(subsample, "All") & x < 0, 
			clcol(black) lwid(thick))
		(lfit legitimatec x if regex(subsample, "All") & x > 0 &
			date!=16396,
			clcol(black) lwid(thick))
		,
			legend(off)
			plotregion(style(none))
			xtitle("Days since election")
			ytitle("Proportion of sample")
			ylab(0(0.2)1, angle(horiz))
			`myxlab'
			xline(0, lpat(dash))
			name(g`i', replace)
			title(`mytitle')
			;
	#delimit cr
	
	gr export "_output/figure_trends_`i'.pdf", replace
	
	gr close

restore

}
